package guitar

import (
	"context"
	"time"

	"go-weixin/app/guitar/api/internal/svc"
	"go-weixin/app/guitar/api/internal/types"

	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
)

type GuitarListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGuitarListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarListLogic {
	return &GuitarListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GuitarListLogic) GuitarList(req *types.GuitarListReq) (resp *types.GuitarListResp, err error) {
	// todo: add your logic here and delete this line
	ctx, cannFunc := context.WithTimeout(context.Background(), time.Second*5)
	defer cannFunc()
	res, err := l.svcCtx.GuitarRPC.GuitarList(ctx, &guitar.GuitarListReq{
		Page: req.Page,
	})
	if err != nil {
		return nil, err
	}
	var guitarList []types.Guitar
	if len(res.GuitarList) > 0 {
		for _, g := range res.GuitarList {
			guitarList = append(guitarList, types.Guitar{
				Id:    g.Id,
				Title: g.Title,
			})
		}
	}
	resp = new(types.GuitarListResp)
	resp.GuitarList = guitarList
	return
}
